package com.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.util.Log;
import android.webkit.MimeTypeMap;
import com.dropbox.client2.DropboxAPI.Entry;
import com.example.gcode.GCloudSingleton;
import com.gcloud.dto.GCloudFile;

public class GCloudUtil {

	public GCloudUtil() {

	}

	public static List<GCloudFile> googleFilesToGCloudFiles(
			List<com.google.api.services.drive.model.File> googleFileList) {

		List<GCloudFile> files = new ArrayList<GCloudFile>();
//		for (com.google.api.services.drive.model.File file : googleFileList) {
//			files.add(new GCloudFile(file.getTitle(),
//					file.getDescription() == null ? "" : file.getDescription(),
//					file.getId(), file.getDownloadUrl() == null ? "" : file
//							.getDownloadUrl(), Constants.GOOGLE, file
//							.getMimeType(), file.getFileSize() == null ? 0
//							: file.getFileSize()));
//		}

		return files;
	}

	public static String getMimeTypeFromURI(String url) {
		String extension = MimeTypeMap.getFileExtensionFromUrl(url);
		Log.i("On utils", MimeTypeMap.getFileExtensionFromUrl(url));
		if (extension == null || extension.equals("")) {
			return getAlternativeMimeType(url);
		}
		MimeTypeMap mimeType = MimeTypeMap.getSingleton();
		return mimeType.getMimeTypeFromExtension(extension);
	}

	static Map<String, String> extensionMap = new HashMap<String, String>();

	private static void fillMymeTypes() {
		GCloudUtil.extensionMap.put("doc", "application/msword");
		GCloudUtil.extensionMap.put("xls", "application/vnd.ms-excel");
		GCloudUtil.extensionMap
				.put("xlsx",
						"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		GCloudUtil.extensionMap.put("xml", "text/xml");
		GCloudUtil.extensionMap.put("ods",
				"application/vnd.oasis.opendocument.spreadsheet");
		GCloudUtil.extensionMap.put("csv", "text/plain");
		GCloudUtil.extensionMap.put("tmpl", "text/plain");
		GCloudUtil.extensionMap.put("pdf", "application/pdf");
		GCloudUtil.extensionMap.put("php", "application/x-httpd-php");
		GCloudUtil.extensionMap.put("jpg", "image/jpeg");
		GCloudUtil.extensionMap.put("png", "image/png");
		GCloudUtil.extensionMap.put("gif", "image/gif");
		GCloudUtil.extensionMap.put("bmp", "image/bmp");
		GCloudUtil.extensionMap.put("txt", "text/plain");

		GCloudUtil.extensionMap.put("js", "text/js");
		GCloudUtil.extensionMap.put("swf", "application/x-shockwave-flash");
		GCloudUtil.extensionMap.put("mp3", "audio/mpeg");
		GCloudUtil.extensionMap.put("zip", "application/zip");
		GCloudUtil.extensionMap.put("rar", "application/rar");
		GCloudUtil.extensionMap.put("tar", "application/tar");
		GCloudUtil.extensionMap.put("arj", "application/arj");
		GCloudUtil.extensionMap.put("cab", "application/cab");
		GCloudUtil.extensionMap.put("html", "text/html");
		GCloudUtil.extensionMap.put("htm", "text/html");
		GCloudUtil.extensionMap.put("default", "application/octet-stream");
		GCloudUtil.extensionMap.put("folder",
				"application/vnd.google-apps.folder");
	}

	private static String getAlternativeMimeType(String url) {
		fillMymeTypes();
		String extension = url.substring(url.length() - 3, url.length());
		extension = extension.trim();
		Log.i("Alternative extension: ", extension);
		// if(extensionMap.containsKey(extension)){

		return GCloudUtil.extensionMap.get(extension);
		// }
		// return "image/jpeg";

	}

	public static List<GCloudFile> dropBoxFilesToGCloudFiles(Entry entry) {
		List<GCloudFile> files = new ArrayList<GCloudFile>();
//		for (Entry file : entry.contents) {
//			files.add(new GCloudFile(file.fileName(), "", file.rev,
//					file.mimeType, file.path == null ? "" : file.path,
//					Constants.DROPBOX, 0, file.isDir));
//		}
		return files;
	}

	public static GCloudFile getSelected(String key) {
		GCloudSingleton gCloudSingleton = GCloudSingleton.getInstance();
		for (GCloudFile file : gCloudSingleton.getFilesFromAllService()) {
			if (file.getKey().equalsIgnoreCase(key)) {
				return file;
			}
		}
		return null;
	}

	public static com.google.api.services.drive.model.File gCloudFileToGoogleDrive(
			GCloudFile file) {
		com.google.api.services.drive.model.File googleFile = new com.google.api.services.drive.model.File();
		googleFile.setId(file.getKey());
//		googleFile.setDownloadUrl(file.getUri().getPath());
		googleFile.setDescription(file.getDescription());
		googleFile.setTitle(file.getName());
		googleFile.setMimeType(file.getMimeType());
		googleFile.setFileSize(file.getSize());
		return googleFile;
	}

}
